package com.lanou.dllo.shise.com.lanou.dllo.receiver;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;

import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.activity.MainActivity;

import cn.jpush.android.api.JPushInterface;

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("lanou", "---收到广播了 MyBroadcastReceiver--");
        Log.i("lanou", "广播intent对象: "+intent);
        //如果收到的是自定义消息
        if (intent.getAction().equals(JPushInterface.ACTION_MESSAGE_RECEIVED)){
            Bundle bundle = intent.getExtras();
            String data = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            Log.i("lanou","---自定义消息内容为: ---"+data);
            if (data != null) {
                showNotification(context,data);
            }
        }
        //判断接收到的这条广播是否是有序广播
        if (isOrderedBroadcast()){
            setResultData("这是由MyBroadcastReceiver传递过来的");
        }


    }
     /**收到自定义消息之后 在通知栏上显示该消息的Notification*/
    private void showNotification(Context context, String data) {
        //获得一个NotificationManager对象
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        builder.setAutoCancel(true);
        builder.setSmallIcon(R.mipmap.icon);
        builder.setTicker("来新消息了");
        builder.setContentText(data);
        builder.setContentTitle("食色正在运行");

        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity
                (context, 100, intent,PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pi);
        managerCompat.notify(2, builder.build());

    }
}
